
* +++++++++++++++++++++
* BUILD CROSSWALK
* POSTAL CODE (PLZ)
* --> COUNTY (KREIS)
* +++++++++++++++++++++

/*
input files come from intersection of Kreis and PLZ shapefile in QGIS
*/

* intersection kreis and plz from QGIS 
import delimited "${data_raw}/geo/cw_krs_plz.csv", clear

bys plz : egen total_area_plz = total(area)
gen share_of_area = area / total_area_plz
bys plz : egen max_share_of_area = max(share_of_area)
keep if max_share_of_area == share_of_area

keep plz ags gen
ren gen place_name

order plz ags place_name 
gen method = "area"

tempfile area
save `area'

* assignment of PLZ to Kreis based on centroids of PLZ
import delimited "${data_raw}/geo/cw_krs_plz_points.csv", clear

isid plz
keep plz ags gen 
ren gen place_name
order plz ags place_name 

* combine the two different assignments 
* prioritize area based approach if it exists
gen method = "points"
append using `area'

bys plz (method) : keep if _n==1
drop method

* save
save "${data_derived}/cw_krs_plz_clean.dta", replace


